package net.ifilm.utility;

import net.ifilm.entity.Film;
import net.ifilm.entity.FilmPath;
import net.ifilm.entity.FilmType;
import net.ifilm.entity.Star;

import java.util.ArrayList;
import java.util.List;

/**
 * User: <a href="wangjunneil@gmail.com">wangjun</a>
 * Date: 2012-08-11 23:47
 */
public class StringUtil {

	/**
	 * 字符串去除首尾空格
	 *
	 * @param targetStr 目标字符串
	 * @return  
	 */
	public static String trim(String targetStr) {
		if (targetStr == null)
			throw new IllegalArgumentException("invalid argument");
		
		return targetStr.trim();
	}

    /**
     * 过滤替换字符串中HTML的特殊文本
     *
     * @param content
     * @return
     */
    public static String filterHTML(String content) {
        content = content.replaceAll("&", "&amp");
        content = content.replaceAll("'", "");
        content = content.replaceAll("<", "&lt");
        content = content.replaceAll(">", "&gt");
        content = content.replaceAll("\"", "&quot");
        content = content.replaceAll(";", "");
        content = content.replaceAll("\n", "<br/>");
        content = content.replaceAll(" ", "&nbsp");
        return content;
    }
















    /**
     * 根据电影名称关键字判断电影是否为高清电影
     *
     * @param filmName 电影网络qvod路径
     * @return 0，DVD：普通清晰版；1，BD：高清无水印版；2，HD：高清版；3，TS：抢先非清晰版
     */
    public static int clearFilm(String filmName) {
        if (filmName.indexOf("DVD") != -1 || filmName.indexOf("dvd") != -1) {
            return 0;
        } else if (filmName.indexOf("BD") != -1 || filmName.indexOf("bd") != -1) {
            return 1;
        } else if (filmName.indexOf("HD") != -1 || filmName.indexOf("hd") != -1) {
            return 2;
        } else if (filmName.indexOf("TS") != -1 || filmName.indexOf("ts") != -1) {
            return 3;
        } else {
            return 0;
        }
    }

    public static List<String> splitString(String content, String key) {
        if (content == null || "".equals(content))
            throw new IllegalArgumentException("invalid argument content");
        if (key == null || "".equals(key))
            throw new IllegalArgumentException("invalid argument key");

        List<String> contentList = new ArrayList<String>();
        if (content.indexOf(key) == -1) {
            contentList.add(content.trim());
            return contentList;
        }

        String [] contents = content.split(key);
        for (String con : contents) {
            contentList.add(con.trim());
        }

        return contentList;
    }

    public static List<String> splitStar(String content) {
        return splitString(content, ",");
    }


    public static String formatStarList(List<Star> starList) {
        StringBuffer sb = new StringBuffer();
        for (Star star : starList) {
            sb.append(star.getStarName()).append(' ');
        }
        return sb.toString().trim();
    }
    
    public static String formatFilmTypeList(List<FilmType> filmTypeList) {
        StringBuffer sb = new StringBuffer();
        for (FilmType filmType : filmTypeList) {
            sb.append(filmType.getTypeName()).append(' ');
        }
        return sb.toString().trim();
    }
    
    public static String formatFilmPathList(List<FilmPath> filmPathList) {
        StringBuffer sb = new StringBuffer();
        FilmPath filmPath = null;
        for (int i = 0; i < filmPathList.size(); i++) {
            filmPath = filmPathList.get(i);
            sb.append(filmPath.getOrderNum()).append(": ").append(filmPath.getNetPath());
            if (i != filmPathList.size() - 1) sb.append("<br/>");
        }
        return sb.toString();
    }


    public static String[] splitStr(String target, String regex) {
        if (target == null || "".equals(target)) {
            throw new IllegalArgumentException("Invalid argument.");
        }

        if (regex == null || "".equals(regex)) {
            throw new IllegalArgumentException("Invalid argument.");
        }

        int index = target.indexOf(regex);
        if (index == -1) return new String[]{target};
        return target.split(regex);
    }
}
